#include "client_transfile.h"

int recvN(int netfd, void* buf, int length)
{
    int total = 0;       
    char* p = (char*)buf;
    while(total < length)
    {
        int ret = recv(netfd, p + total, length - total, 0);
        if(ret == 0)
        {
            return -1;
        }
        total += ret;
    }

    return 0;
}
int recvFile(int netfd, const char* filename) {
    int exist_flag;
    int length;
    recvN(netfd, &length, sizeof(int));
    recvN(netfd, &exist_flag, length);

    if(exist_flag == 0) {
        printf("服务端文件不存在\n");
        return 0;
    }

    int fd = open(filename, O_WRONLY | O_CREAT | O_APPEND , 0666);
    struct stat statbuf;
    fstat(fd, &statbuf);

    Train train;
    train.length = sizeof(statbuf.st_size);
    memcpy(train.data, &statbuf.st_size, train.length);
    send(netfd, &train, sizeof(int) + train.length, MSG_NOSIGNAL);

    length = sizeof(int);
    off_t filesize;
    recvN(netfd, &length, sizeof(int));
    recvN(netfd, &filesize, length);
    printf("提示信息: filesize = %ld\n", filesize);

    off_t done_size = 0;
    off_t last_size = 0;
    off_t slice = filesize / 100;
    int percentage = 0;

    char buf[1024] = {0};
    while(1)
    {
        recvN(netfd, &length, sizeof(int));
        if(length == 0)
        {
            break;
        }
        done_size += length;
        if(done_size - last_size >= slice) {
            printf("%5.2lf%%\r", 100.0 * done_size / filesize);
            fflush(stdout);
            last_size = done_size;
        }
        bzero(buf, sizeof(buf));
        recvN(netfd, buf, length);
        write(fd, buf, length);
    }
    printf("100.00%%\n");

    close(fd);

    return 0;
}
int sendFile(int netfd, const char* filename) {
    Train train;
    bzero(&train, sizeof(train));

    int fd = open(filename, O_RDONLY);
    train.length = sizeof(fd);
    memcpy(train.data, &fd, sizeof(fd));
    send(netfd, &train, sizeof(int) + train.length, 0);
    if(fd == -1) {
        printf("本地文件不存在\n");
        return 0;
    }

    int exist_flag;
    int length;
    recvN(netfd, &length, sizeof(int));
    recvN(netfd, &exist_flag, length);

    if(exist_flag == 1) {
        printf("服务端文件已存在\n");
        return 0;
    }

    char md5[1024] = {0};
    char true_path[1024] = {0};
    strcpy(true_path, "./");
    strcat(true_path, filename);
    Compute_file_md5(true_path, md5);
    printf("md5=%s\n",md5);
    bzero(&train, sizeof(train));
    train.length = strlen(md5);
    memcpy(train.data, md5, train.length);
    send(netfd, &train, sizeof(int) + train.length, 0);

    int quick_flag;
    recvN(netfd, &length, sizeof(int));
    recvN(netfd, &quick_flag, length);

    if(quick_flag == 1) {
        printf("提示信息: 秒传成功\n");
        return 0;
    }

    printf("提示信息: 不秒传\n");

    struct stat statbuf;
    fstat(fd, &statbuf);
    off_t filesize = statbuf.st_size;
    off_t done_size = 0;
    off_t last_size = 0;
    off_t slice = filesize / 100;
    int percentage = 0;

   /* while(1) {
        bzero(&train, sizeof(train));
        train.length = read(fd, train.data, sizeof(train.data));
        send(netfd, &train, sizeof(int) + train.length, 0);
        done_size += train.length;
        if(done_size - last_size >= slice) {
            printf("%5.2lf%%\r", 100.0 * done_size / filesize);
            fflush(stdout);
            last_size = done_size;
        }
        if(train.length == 0) {
            break;
        }
    }*/
    sendfile(netfd,fd,NULL,statbuf.st_size);
    printf("100.00%%\n");

    close(fd);

    return 0;
}
